package com.edu.hre.db.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;



import com.edu.hre.db.config.BaseDAO;
import com.edu.hre.db.config.DBPool;
import com.edu.hre.db.dto.HreEmpBasicsalary;
import com.edu.hre.db.dto.HreEmpEmergencyContact;
import com.edu.hre.db.dto.HreEmpDependent;
import com.edu.hre.db.dto.HreEmpWorkExperience;


public class EmpWorkExperienceDAO  extends BaseDAO {
	private static final Logger logger = Logger.getLogger(EmpWorkExperienceDAO.class);
	@SuppressWarnings("deprecation")
	public List<HreEmpWorkExperience> getExperienceByEmployeeId(
			int employeeNumber) {
		List<HreEmpWorkExperience> listExperience = new ArrayList<HreEmpWorkExperience>();
		Connection conn = null;
		PreparedStatement preStmt = null;
		ResultSet rs = null;
		try {
			conn = DBPool.getConnection();
			String strSQLQuery = "SELECT * FROM hre_emp_work_experience "+
			"WHERE emp_number = "
					+ employeeNumber;
			preStmt = conn.prepareStatement(strSQLQuery);
			rs = preStmt.executeQuery();
			while (rs.next()) {
				HreEmpWorkExperience experience = new HreEmpWorkExperience();
				experience.setEexpEmployer(rs.getString("eexp_employer"));
				experience.setEexpJobtit(rs.getString("eexp_jobtit"));
				Date d = null;
				if(rs.getDate("eexp_from_date") != null){
					d= (rs.getDate("eexp_from_date"));
					d.setYear(d.getYear()-1900);
				}
				experience.setEexpFromDate(d);
				if(rs.getDate("eexp_to_date") != null){
					d= (rs.getDate("eexp_to_date"));
					d.setYear(d.getYear()-1900);
				}
				experience.setEexpToDate(d);
				experience.setEexpComments(rs.getString("eexp_comments"));
				//.....
				//.....
				listExperience.add(experience);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		} finally {
			// đóng PreparedStatement, ResultSet và đẩy connection vào pool
			releaseConnection(conn, preStmt, rs);
		}
		return listExperience;
	}

}
